package com.xgit.iov.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xgit.iov.admin.service.ISysTenantService;
import com.xgit.iov.common.core.entity.SysTenant;
import com.xgit.iov.common.core.util.R;
import com.xgit.iov.common.log.annotation.LogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 租户管理
 *
 * @author william
 * @date 2020-07-04
 */

@Api(tags = "租户模块")
@RestController
@RequestMapping("/tenant")
public class SysTenantController {

    @Autowired
    private ISysTenantService sysTenantService;

    /**
     * 查询租户列表
     */
    @ApiOperation(value = "查询租户列表")
    @GetMapping("/page")
    public R page(Page page, SysTenant sysTenant) {
        return R.ok(sysTenantService.selectSysTenantPage(page,sysTenant));
    }

	/**
	 * 获取全部租户
	 */
	@ApiOperation(value = "获取全部租户")
	@GetMapping("/list")
	public R list() {
		return R.ok(sysTenantService.list(Wrappers.emptyWrapper()));
	}

    /**
     * 获取租户详细信息
     */
    @ApiOperation(value = "获取租户详细信息")
    @GetMapping(value = "/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(sysTenantService.selectSysTenantById(id));
    }

    /**
     * 新增租户
     */
    @ApiOperation(value = "新增租户")
    @LogAnnotation("新增租户")
    @PostMapping(value = "/save")
    @PreAuthorize("@pms.hasPermission('admin_tenant_add')")
    public R save(@Valid @RequestBody SysTenant sysTenant) {
        return R.ok(sysTenantService.insertSysTenant(sysTenant));
    }

    /**
     * 修改租户
     */
    @ApiOperation(value = "修改租户")
    @LogAnnotation("修改租户")
    @PutMapping(value = "/update")
    @PreAuthorize("@pms.hasPermission('admin_tenant_edit')")
    public R update(@Valid @RequestBody SysTenant sysTenant) {
        return R.ok(sysTenantService.updateSysTenant(sysTenant));
    }

    /**
     * 删除租户
     */
    @ApiOperation(value = "删除租户")
    @LogAnnotation("删除租户")
	  @DeleteMapping("/{ids}")
    @PreAuthorize("@pms.hasPermission('admin_tenant_del')")
    public R remove(@PathVariable Long[] ids) {
        return R.ok(sysTenantService.deleteSysTenantByIds(ids));
    }
}
